package com.sinothk.fc.bo.supply.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sinothk.fc.bo.supply.entity.BoSupplyEntity;
import com.sinothk.fc.bo.supply.entity.BoSupplyOrderVO;
import com.sinothk.fc.bo.supply.entity.BoSupplyVO;
import com.sinothk.fc.bo.supply.service.IBoSupplyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

/**
 * @Description: 商机供应
 * @Author: Liangyt
 * @Date: 2022-10-23
 * @Version: V1.0
 */
@Api(tags = "商机供应")
@RestController
@RequestMapping("/fc/boSupplyCtrl")
@Slf4j
public class BoSupplyController extends JeecgController<BoSupplyEntity, IBoSupplyService> {

    private final IBoSupplyService boSupplyService;

    public BoSupplyController(IBoSupplyService boSupplyService) {
        this.boSupplyService = boSupplyService;
    }

    /**
     * 分页列表查询
     *
     * @param boSupply
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "商机供应-分页列表查询")
    @ApiOperation(value = "商机供应-分页列表查询", notes = "商机供应-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(BoSupplyEntity boSupply,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<BoSupplyEntity> queryWrapper = QueryGenerator.initQueryWrapper(boSupply, req.getParameterMap());
        Page<BoSupplyEntity> page = new Page<BoSupplyEntity>(pageNo, pageSize);
        IPage<BoSupplyEntity> pageList = boSupplyService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 登录人收到的邀请商品列表
     *
     * @param demandId
     * @param pageNo
     * @param pageSize
     * @return
     */
    @AutoLog(value = "商机供应-登录人收到的邀请商品列表")
    @ApiOperation(value = "商机供应-登录人收到的邀请商品列表", notes = "商机供应-登录人收到的邀请商品列表")
    @GetMapping(value = "/demandSupplyGoodsForDemandIdPage")
    public Result<?> demandSupplyGoodsForDemandIdPage(@RequestParam(name = "demandId") String demandId,
                                                      @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                      @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) throws Exception {
        IPage<BoSupplyVO> pageList = boSupplyService.demandSupplyGoodsForDemandIdPage(new Page<>(pageNo, pageSize), demandId);
        return Result.OK(pageList);
    }

    /**
     * 登录人发送的邀请-收到的中标列表
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @AutoLog(value = "登录人发送的邀请-收到的中标列表")
    @ApiOperation(value = "登录人发送的邀请-收到的中标列表", notes = "登录人发送的邀请-收到的中标列表")
    @GetMapping(value = "/supplyGoodsOrderPage")
    public Result<?> supplyGoodsOrderPage(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                          @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) throws Exception {
        IPage<BoSupplyOrderVO> pageList = boSupplyService.supplyGoodsOrderPage(new Page<>(pageNo, pageSize));
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param boSupply
     * @return
     */
    @AutoLog(value = "商机供应-添加")
    @ApiOperation(value = "商机供应-添加", notes = "商机供应-添加")
    @PostMapping(value = "/add")
    public Result<BoSupplyEntity> add(@RequestBody BoSupplyEntity boSupply) {
        boSupplyService.save(boSupply);
        return Result.OK(boSupply);
    }

    /**
     * 编辑
     *
     * @param boSupply
     * @return
     */
    @AutoLog(value = "商机供应-编辑")
    @ApiOperation(value = "商机供应-编辑", notes = "商机供应-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody BoSupplyEntity boSupply) {
        boSupplyService.updateById(boSupply);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "商机供应-通过id删除")
    @ApiOperation(value = "商机供应-通过id删除", notes = "商机供应-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        boSupplyService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "商机供应-批量删除")
    @ApiOperation(value = "商机供应-批量删除", notes = "商机供应-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.boSupplyService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "商机供应-通过id查询")
    @ApiOperation(value = "商机供应-通过id查询", notes = "商机供应-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        BoSupplyEntity boSupply = boSupplyService.getById(id);
        if (boSupply == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(boSupply);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param boSupply
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, BoSupplyEntity boSupply) {
        return super.exportXls(request, boSupply, BoSupplyEntity.class, "商机供应");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, BoSupplyEntity.class);
    }

}
